const roleDao = require('../service/role_dao')

module.exports = {
      // 角色列表
      rolelist: async (ctx, next) => {
            let body = await roleDao.getAllRole();
            await ctx.render('role/rolelist', {
                  body: body
            })
      },
      // 角色添加
      add: async (ctx, next) => {
            await ctx.render('role/add');
      },
      doAdd: async (ctx, next) => {
            var addResult = ctx.request.body;
            var oneRoleInfo = await roleDao.getOneRoleInfo(addResult.role_name, '');
            if (oneRoleInfo) {
                  $("#rodeAlert").attr("class", "alert-warning");
                  await ctx.redirect('/roles/add', {
                        result: true
                  })
            } else {
                  if (addResult.role_name && addResult.description) {
                        await roleDao.postRole(addResult);
                        await ctx.redirect('/roles/rolelist', {
                              result: true
                        })
                  } else {
                        await ctx.redirect('/roles/add', {
                              result: true
                        })
                  }
            }
      },

      // 用户编辑
      edit: async (ctx, next) => {
            //获取编辑的数据
            const role_id = ctx.request.query.id;
            const roleResult = await roleDao.getRoleInfo(role_id);
            await ctx.render('role/edit', {
                  roleResult: roleResult
            });
      },
      doEdit: async (ctx, next) => {
            var editResult = ctx.request.body;
            roleDao.putRole(editResult); 
            await ctx.redirect('/roles/rolelist', {})
      },
      // 角色删除
      delect: async (ctx, next) => {
            let b = ctx.params.b
            console.log(b)
            roleDao.delectRole(b)
            await ctx.redirect('/roles/rolelist', {})
      },
}